﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.OleDb;
using System.Configuration;
using Newtonsoft.Json;

namespace MechanicsLab
{
    public class LiNengPress
    {
        string connStr = "";

        public LiNengPress(string path)
        {
            connStr = @"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + path + ";Persist Security Info=False;";
        }

        #region 万能机
        public DataTable SelPostTou(string precativeNum)
        {
            DataTable dt = null;
            try
            {
                string sql = "select * from GroupInfo where GroupName='" + precativeNum + "' and State=2";
                dt = Query(sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }


        public DataTable SelPostBaoTi(string GroupID)
        {
            DataTable dt = null;
            try
            {
                string sql = "select * from TestInfo where GroupID='" + GroupID + "' and State=2";
                dt = Query(sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }

        public DataTable SelPostBaoTiInfo(string TestID,string ParamName)
        {
            DataTable dt = null;
            try
            {
                string sql = "select * from TestParam where TestID='" + TestID + "' and ParamType=3 and ParamName='" + ParamName + "'";
                dt = Query(sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }


        public DataTable SelPostBaotiCurre(string TestID)
        {
            DataTable dt = null;
            try
            {
                string sql = "select * from TestData where TestID='" + TestID + "'";
                dt = Query(sql);
            }
            catch (Exception ex)
            {
                throw ex;
            }
            return dt;
        }


        public string PostData(string precativeNum, string name, string remark_1, string GroupID)
        {
            List<PostDataPress> postdata = new List<PostDataPress>();
            PostDataPress chp = new PostDataPress();
            chp.trialName = name;
            chp.equNum = ConfigurationManager.AppSettings["Number"];
            //一组数据ID
            chp.ID = remark_1;
            //试验单号
            chp.Number = precativeNum;
            DataTable dr1 = SelPostBaoTi(GroupID);
            if (dr1 != null)
            {
                if (dr1.Rows.Count > 0)
                {
                    for (int i = 0; i < dr1.Rows.Count; i++)
                    {
                        switch (i.ToString())
                        { 
                            case "0":
                                chp.Force1 = dr1.Rows[i]["MaxLoad"].ToString();
                                chp.Bendi1 = SelPostBaoTiInfo(dr1.Rows[i]["TestID"].ToString(), "下屈服").Rows[0]["ParamValue"].ToString();
                                chp.Compress1 = SelPostBaoTiInfo(dr1.Rows[i]["TestID"].ToString(), "抗拉强度").Rows[0]["ParamValue"].ToString();
                                chp.Flexural1 = SelPostBaoTiInfo(dr1.Rows[i]["TestID"].ToString(), "下屈服强度").Rows[0]["ParamValue"].ToString();
                                DataTable dr2 = SelPostBaotiCurre(dr1.Rows[i]["TestID"].ToString());
                                for (int j = 0; j < dr2.Rows.Count; j++)
                                {
                                    chp.Curve1.Add(dr2.Rows[j]["TimeData"].ToString() + "," + dr2.Rows[j]["TimeData"].ToString());
                                }
                                break;
                            case "1":
                                chp.Force2 = dr1.Rows[i]["MaxLoad"].ToString();
                                chp.Bendi2 = SelPostBaoTiInfo(dr1.Rows[i]["TestID"].ToString(), "下屈服").Rows[0]["ParamValue"].ToString();
                                chp.Compress2 = SelPostBaoTiInfo(dr1.Rows[i]["TestID"].ToString(), "抗拉强度").Rows[0]["ParamValue"].ToString();
                                chp.Flexural2 = SelPostBaoTiInfo(dr1.Rows[i]["TestID"].ToString(), "下屈服强度").Rows[0]["ParamValue"].ToString();
                                DataTable dr3 = SelPostBaotiCurre(dr1.Rows[i]["TestID"].ToString());
                                for (int j = 0; j < dr3.Rows.Count; j++)
                                {
                                    chp.Curve2.Add(dr3.Rows[j]["TimeData"].ToString() + "," + dr3.Rows[j]["TimeData"].ToString());
                                }
                                break;
                            case "2":
                                chp.Force3 = dr1.Rows[i]["MaxLoad"].ToString();
                                chp.Bendi3 = SelPostBaoTiInfo(dr1.Rows[i]["TestID"].ToString(), "下屈服").Rows[0]["ParamValue"].ToString();
                                chp.Compress3 = SelPostBaoTiInfo(dr1.Rows[i]["TestID"].ToString(), "抗拉强度").Rows[0]["ParamValue"].ToString();
                                chp.Flexural3 = SelPostBaoTiInfo(dr1.Rows[i]["TestID"].ToString(), "下屈服强度").Rows[0]["ParamValue"].ToString();
                                DataTable dr4 = SelPostBaotiCurre(dr1.Rows[i]["TestID"].ToString());
                                for (int j = 0; j < dr4.Rows.Count; j++)
                                {
                                    chp.Curve3.Add(dr4.Rows[j]["TimeData"].ToString() + "," + dr4.Rows[j]["TimeData"].ToString());
                                }
                                break;
                            default:
                                break;
                        }
                    }
                }
            }
            postdata.Add(chp);
            string jsonstr = JsonConvert.SerializeObject(postdata);
            string str = Pub.HttpPost(ConfigurationManager.AppSettings["PostDataUrl"], jsonstr);
            return str;
        }
        #endregion


        public DataTable Query(string sql)
        {
            OleDbConnection conn = null;
            try
            {
                DataTable table = new DataTable();
                conn = new OleDbConnection(connStr);
                conn.Open();
                using (OleDbDataAdapter da = new OleDbDataAdapter(sql, conn))
                {
                    da.Fill(table);
                }
                return table;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
